using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data;

namespace DataAccessLayer
{
    public  class Query
    {
        private int UserId;
        private string Title;
        private string Interest;
        private string QueryStatus;
        private string queryBody;
        private string connstring;

        public Query(string connstring)
        {
            this.connstring = connstring;
        }
        public int UID
        {
            get { return UserId; }
            set { UserId = value; }
        }
        public string TITLE
        {
            get { return Title; }
            set { Title = value; }
        }
        public string INTEREST
        {
            get { return Interest; }
            set { Interest = value; }
        }
        public string QSTATUS
        {
            get { return QueryStatus; }
            set { QueryStatus = value; }
        }
        public string QBODY
        {
            get { return queryBody; }
            set { queryBody = value; }
        }
        public void PostQuery()
        {
            SqlConnection con = new SqlConnection(connstring);
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "sp_post_query";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@uid", UserId));
            cmd.Parameters.Add(new SqlParameter("@title", Title));
            cmd.Parameters.Add(new SqlParameter("@interset", Interest));
            cmd.Parameters.Add(new SqlParameter("@Qstatus", QueryStatus));
            cmd.Parameters.Add(new SqlParameter("@qbody", queryBody));
            cmd.ExecuteNonQuery();
            con.Close();
        }
        public int fetchUserID(string uemail)
        {
            SqlConnection con = new SqlConnection(connstring);
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "sp_find_user";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@uemail", uemail));

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ds.Clear();
            da.Fill(ds);
            string  userid = ds.Tables[0].Rows[0]["UserId"].ToString();
            int uid = Convert.ToInt32(userid);
            return uid;
        }
        public DataTable Load()
        {
            SqlDataReader s1 = null;
            SqlConnection con = new SqlConnection(connstring);
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "sp_show";
            cmd.CommandType = CommandType.StoredProcedure;
            s1 = cmd.ExecuteReader();
            DataTable dtDisplay = new DataTable();
            dtDisplay.Load(s1);
            con.Close();
            return dtDisplay;
        }
    }
}
